package com.dehui.traders.ui.viewmodle;

import android.content.Context;
import android.databinding.ObservableArrayList;
import android.databinding.ObservableBoolean;
import android.databinding.ObservableList;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.support.annotation.NonNull;
import android.util.Log;

import com.dehui.traders.R;
import com.dehui.traders.bean.ConstantTest;
import com.dehui.traders.kchart.CoupleChartGestureListener;
import com.dehui.traders.kchart.DataParse;
import com.dehui.traders.kchart.KLineBean;
import com.dehui.traders.kchart.KLineManager;
import com.dehui.traders.kchart.MyBottomMarkerView;
import com.dehui.traders.kchart.MyCombinedChart;
import com.dehui.traders.kchart.MyHMarkerView;
import com.dehui.traders.kchart.MyLeftMarkerView;
import com.dehui.traders.kchart.VolumeManager;
import com.dehui.traders.ui.activity.AddOptionalStockActivity;
import com.dehui.traders.ui.fragment.OptionalStockManagerFragment;
import com.github.mikephil.charting.charts.Chart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.CandleData;
import com.github.mikephil.charting.data.CandleDataSet;
import com.github.mikephil.charting.data.CombinedData;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.github.mikephil.charting.utils.ViewPortHandler;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import rx.functions.Action0;

/**
 * Created by hqq on 18.4.4.
 */

public class BuySellViewModel extends BaseViewModel{

    public ObservableList<ObservableBoolean> selectList = new ObservableArrayList<>();
    public ObservableBoolean tv1SelectObservable = new ObservableBoolean(false);
    public ObservableBoolean tv2SelectObservable = new ObservableBoolean(false);
    public ObservableBoolean tv3SelectObservable = new ObservableBoolean(false);
    public ObservableBoolean tv4SelectObservable = new ObservableBoolean(false);
    public ObservableBoolean tv5SelectObservable = new ObservableBoolean(false);
    public ObservableBoolean tv6SelectObservable = new ObservableBoolean(false);
    private DataParse mData;

    public BuySellViewModel(Context context) {
        super(context);
        init();
    }

    private void init() {
        selectList.add(tv1SelectObservable);
        selectList.add(tv2SelectObservable);
        selectList.add(tv3SelectObservable);
        selectList.add(tv4SelectObservable);
        selectList.add(tv5SelectObservable);
        selectList.add(tv6SelectObservable);
    }

    public BindingCommand addOptionalStockOnClickCommand = new BindingCommand(()->{
        startActivity(AddOptionalStockActivity.class);
    });

    public BindingCommand managerOptionalStockOnClickCommand = new BindingCommand(()->{
        startContainerActivity(OptionalStockManagerFragment.class.getCanonicalName());
    });

    private void resetSelectList() {
        for (ObservableBoolean item : selectList){
            item.set(false);
        }
    }

    public BindingCommand tv1onClickCommand = new BindingCommand(()->{
        resetSelectList();
        tv1SelectObservable.set(true);
    });

    public BindingCommand tv2onClickCommand = new BindingCommand(()->{
        resetSelectList();
        tv2SelectObservable.set(true);
    });

    public BindingCommand tv3onClickCommand = new BindingCommand(()->{
        resetSelectList();
        tv3SelectObservable.set(true);
    });

    public BindingCommand tv4onClickCommand = new BindingCommand(()->{
        resetSelectList();
        tv4SelectObservable.set(true);
    });

    public BindingCommand tv5onClickCommand = new BindingCommand(()->{
        resetSelectList();
        tv5SelectObservable.set(true);
    });

    public BindingCommand tv6onClickCommand = new BindingCommand(()->{
        resetSelectList();
        tv6SelectObservable.set(true);
    });

    @SuppressWarnings("unchecked")
    public void initChart(MyCombinedChart chartKline, MyCombinedChart chartVolume) {
        getOffLineData();
        KLineManager.getInstange().initKLine(context,chartKline,mData).setKLineNoBorder();
        VolumeManager.getInstange().initVolume(context,chartVolume,mData);

        chartKline.setOnChartGestureListener(new CoupleChartGestureListener(chartKline, new Chart[]{chartVolume}));
        chartVolume.setOnChartGestureListener(new CoupleChartGestureListener(chartVolume, new Chart[]{chartKline}));
    }

    @SuppressWarnings("all")
    private void getOffLineData() {
        /*方便测试，加入假数据*/
        mData = new DataParse();
        JSONObject object = null;
        try {
            object = new JSONObject(ConstantTest.KLINEURL);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Log.d("数据-----", object.toString());
        mData.parseKLine(object);
    }



}
